import api from "../../utils/api"

Component({
  properties: {
    mirrorInfos: {
      type: Array,
      value: []
    },
    imgUrl: {
      type: String,
      value: ''
    },
    skuGroupInfos: {
      type: Array,
      value: []
    }
  },
  attached: function () {
    console.log(this.data.skuGroupInfos, 'skuGroupInfos==================')
    let skuLength = this.data.skuGroupInfos.length
    let chooseId = []
    for (let i = 0; i < skuLength; i++) {
      chooseId.push('')
    }
    this.setData({
      chooseId: chooseId
    })
  },
  data: {
    // 这里是一些组件内部数据
    someData: {},
    chooseId: null,
    chooseType: {},
    isFullSucreen: false,
    cityList: [],
    cityChoose: '',
    canUseList: [],
    buttonType: ''
  },
  methods: {
    // 这里是一个自定义方法
    cancelDiaglog() {
      this.triggerEvent("cancelDiaglog")
    },
    moveToOrder() {
      let chooseType = this.data.chooseType
      console.log(chooseType)
      if (chooseType.hasOwnProperty('id') && chooseType.id) {
        this.triggerEvent("moveToOrder", chooseType)
      } else {
        wx.showToast({
          title: '请选择服务套餐',
          icon: 'error'
        })
      }
    },
    stop() {
      console.log('stop')
      return
    },
    typeChoose(e) {
      let id = e.currentTarget.dataset.id
      let index = e.currentTarget.dataset.index
      let chooseId = this.data.chooseId
      chooseId[index] = id
      let canUseList = this.data.mirrorInfos
      canUseList.forEach((item, canIndex) => {
        for (let i = 0; i < chooseId.length; i++) {
          if (item.skuid.indexOf(chooseId[i]) < 0 && item.skuid.indexOf(chooseId[i] - 1)) {
            console.log(canIndex)
          }
        }
      })
      console.log(canUseList, 'canUseList==================')

      this.setData({
        chooseId: chooseId
      })
      // this.setData({
      //   chooseId: id,
      //   chooseType: chooseType,
      //   buttonType: ''
      // }, () => {
      //   this.hasSubscribe()
      // })
    },
    chooseCity(e) {
      let cityChoose = e.currentTarget.dataset.provinceid
      let canUseList = []
      this.data.mirrorInfos.forEach(item => {
        if (item.provinceId === cityChoose) {
          canUseList.push(item)
        }
      })
      this.setData({
        cityChoose: cityChoose,
        canUseList: canUseList,
        chooseType: {
          payPrice: '',
          areaName: ''
        },
        buttonType: '',
        chooseId: ''
      })
    },
    hasSubscribe() {
      let effectiveNum = this.data.chooseType.effectiveNum * 1
      if (effectiveNum <= 0) {
        api.hasSubscribe({
          data: {
            productMirrorId: this.data.chooseType.id
          },
          success: res => {
            console.log(res.data.hasSubscribe, 'res.data.hasSubscribe==================')
            if (res.code * 1 === 0) {
              if (this.data.buttonType === 'alreadyBtn') {
                console.log('a00000000000')
                wx.showToast({
                  title: '取消订阅',
                  type: 'success',
                  duration: 1500
                })
              }
              this.setData({
                buttonType: res.data.hasSubscribe ? 'alreadyBtn' : 'needBtn'
              })
            }
          }
        })
      }
    },
    subscribe() {
      api.subscribe({
        data: {
          source: 4,
          productMirrorId: this.data.chooseType.id
        },
        success: res => {
          if (res.code * 1 === 0) {
            console.log(this.data.buttonType, '---------------------')
            let that = this
            wx.hideLoading()
            this.hasSubscribe()
            if (this.data.buttonType === 'needBtn') {
              this.triggerEvent('needToRead')
            }

          } else {
            wx.showToast({
              title: '订阅失败，请重新订阅',
              type: 'error',
              duration: 1500
            })
          }
        }
      })
    }
  }
})